package com.dycx.message.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.message.converter.TemplateConverter;
import com.dycx.message.dto.TemplateDTO;
import com.dycx.message.entity.TemplateDO;
import com.dycx.message.enums.MessageTypeEnum;
import com.dycx.message.mapper.TemplateMapper;
import com.dycx.message.service.TemplateService;
import com.dycx.message.vendor.MessageApi;
import com.dycx.message.vo.TemplateVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@AllArgsConstructor
public class TemplateImpl extends ServiceImpl<TemplateMapper, TemplateDO> implements TemplateService {

    private TemplateMapper templateMapper;

    @Override
    public boolean addTemplate(TemplateDTO templateDTO) {
        if (ObjectUtil.isNotNull(getInfoByCode(templateDTO.getCode()))) {
            throw new RuntimeException("模版code已存在，请重新输入");
        }

        TemplateDO templateDO = BeanUtil.toBean(templateDTO, TemplateDO.class);
        return this.save(templateDO);
    }

    @Override
    public boolean updateTemplate(TemplateDTO templateDTO) {
        LambdaQueryWrapper<TemplateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateDO::getCode, templateDTO.getCode());
        wrapper.ne(TemplateDO::getId, templateDTO.getId());
        if (ObjectUtil.isNotNull(getOne(wrapper))) {
            throw new RuntimeException("模版code已存在，请重新输入");
        }

        TemplateDO templateDO = BeanUtil.toBean(templateDTO, TemplateDO.class);
        return this.updateById(templateDO);
    }

    public TemplateVO getInfoByCode(String code) {
        LambdaQueryWrapper<TemplateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateDO::getCode, code);
        return TemplateConverter.entityToVo(getOne(wrapper));
    }

    /**
     * 分页
     *
     * @return
     */
    @Override
    public Page<TemplateVO> getPage(TemplateDTO dto) {
        LambdaQueryWrapper<TemplateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getMessageType()), TemplateDO::getMessageType, dto.getMessageType());
        wrapper.orderByDesc(TemplateDO::getId);

        Page<TemplateDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        return TemplateConverter.entityToVoPage(entityPage);
    }

    /**
     * 查询模版
     *
     * @param ids
     * @return
     */
    @Override
    public List<TemplateVO> getListByIds(List<Integer> ids) {
        LambdaQueryWrapper<TemplateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TemplateDO::getId, ids);
        wrapper.eq(TemplateDO::getStatus, true);

        return TemplateConverter.entityToVoList(list(wrapper));
    }


    /**
     * 获取所有
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getEnableList(Integer messageType) {
        List<Map<String, Object>> templateAllList = new ArrayList<>();
        for (MessageTypeEnum item : MessageTypeEnum.values()) {
            if (ObjectUtil.isNotNull(messageType) && !item.getCode().equals(messageType)) continue;

            LambdaQueryWrapper<TemplateDO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TemplateDO::getMessageType, item.getCode());
            wrapper.eq(TemplateDO::getStatus, true);
            wrapper.select(TemplateDO::getId, TemplateDO::getName);
            List<TemplateVO> templateList = TemplateConverter.entityToVoList(this.list(wrapper));

            Map<String, Object> map = new HashMap<>();
            map.put("name", item.getName());
            map.put("templateList", templateList);

            templateAllList.add(map);
        }

        return templateAllList;
    }

    /**
     * 同步模板
     */
    @Override
    public void syncTemplate() {
        for (MessageTypeEnum item : MessageTypeEnum.values()) {
            List<TemplateVO> templateList = MessageApi.getMessageTemplateList(Convert.toInt(item.getCode()));
            templateList.forEach(templateItem -> {
                TemplateDO templateDO = BeanUtil.toBean(templateItem, TemplateDO.class);
                templateDO.setMessageType(item.getCode());

                TemplateVO templateVO = getInfoByCode(templateItem.getCode());
                if (ObjectUtil.isNull(templateVO)) {
                    save(templateDO);
                } else {
                    templateDO.setId(templateVO.getId());
                    updateById(templateDO);
                }
            });
        }
    }

}
